# -*- coding: utf-8 -*-
#是否开机启动
#设置为开机壁纸 js注入
#保存当前设置到开机启动
from MsgProcess import MsgProcess, MsgType
import os,time,json

class Meitu(MsgProcess):
    TIME=2
    root=0
    start_root=0
    
    def setfill(self,data={"root":0,"time":2},key="r"):
        if key=="r":
            if os.path.exists('/keyicx/python/plugin/Meitu/start.txt'):
                with open("/keyicx/python/plugin/Meitu/start.txt") as f:
                    try:
                        temporary= json.loads(f.read())
                        return temporary
                    except: pass
            return data

        if key=="w":
            os.system("sudo chmod 777 /keyicx/python/plugin/Meitu")
            with open("/keyicx/python/plugin/Meitu/start.txt","w",) as f:
                os.system("sudo chmod 777 /keyicx/python/plugin/Meitu/start.txt")
                f.write(json.dumps(data, indent=2))


    #开机事件
    def Start(self,message=None):
        time.sleep(10)
        self.fill=["/plugin/Meitu/photo/"+x for x in os.listdir("/keyicx/python/plugin/Meitu/photo")]
        self.totalData=self.setfill()
        self.root=self.totalData["root"]
        self.start_root=self.totalData["root"]
        if self.start_root==1 or self.start_root==2:
            self.start_root=1
        self.TIME=self.totalData["time"]
        if self.root==1:
            self.Text({"Data":"背景美图"})
        if self.root==2:
            self.Text({"Data":"全屏美图"})

    def Refresh(self,root,start_root):
        if root==0:
            self.send(MsgType.Text, Receiver='MqttProxy', Data={'action':'Meitu',"MeituTime":"{0}秒".format(self.TIME),"Meitulen":"{0}张".format(len(self.fill)),"MeituStart":start_root,"Meitustate":"开机不启动"})
        if root==1:
            self.send(MsgType.Text, Receiver='MqttProxy', Data={'action':'Meitu',"MeituTime":"{0}秒".format(self.TIME),"Meitulen":"{0}张".format(len(self.fill)),"MeituStart":start_root,"Meitustate":"背景美图"})
        if root==2:
            self.send(MsgType.Text, Receiver='MqttProxy', Data={'action':'Meitu',"MeituTime":"{0}秒".format(self.TIME),"Meitulen":"{0}张".format(len(self.fill)),"MeituStart":start_root,"Meitustate":"全屏美图"})


    def Text(self, message):  
         
        data = message["Data"]

        if "背景" in data:
            self.root=1
            self.fill=["/plugin/Meitu/photo/"+x for x in os.listdir("/keyicx/python/plugin/Meitu/photo")]
            datas = {  "type":"exejs","data":"set_Meitu_bg_play({0})".format( {"data":self.fill,"time":self.TIME,"len":len(self.fill)})   }
            self.send( MsgType=MsgType.Text, Receiver="Screen", Data= datas )
            self.Refresh(self.root,self.start_root)

        if "全屏" in data:
            self.root=2
            self.fill=["/plugin/Meitu/photo/"+x for x in os.listdir("/keyicx/python/plugin/Meitu/photo")]
            datas = {  "type":"exejs","data":"set_Meitu_Fullscreen_play({0})".format( {"data":self.fill,"time":self.TIME,"len":len(self.fill)})   }
            self.send( MsgType=MsgType.Text, Receiver="Screen", Data= datas )
            self.Refresh(self.root,self.start_root)

        if "暂停" in data:
            self.send( MsgType=MsgType.Text, Receiver="Screen", Data= {  "type":"exejs","data":"set_Meitu_suspend()"} )

        if "退出" in data:
            self.send( MsgType=MsgType.Text, Receiver="Screen", Data= {  "type":"exejs","data":"set_Meitu_exit()"} )


        if "+1" in data:
            self.TIME+=1
            if self.root==1:
                datas = {  "type":"exejs","data":"set_Meitu_bg_play({0})".format( {"data":self.fill,"time":self.TIME,"len":len(self.fill)})   }
                self.send( MsgType=MsgType.Text, Receiver="Screen", Data= datas )
            if self.root==2:
                datas = {  "type":"exejs","data":"set_Meitu_Fullscreen_play({0})".format( {"data":self.fill,"time":self.TIME,"len":len(self.fill)})   }
                self.send( MsgType=MsgType.Text, Receiver="Screen", Data= datas )
            self.Refresh(self.root,self.start_root)

        if "-1" in data:
            self.TIME-=1
            if self.TIME<=1:self.TIME=1
            if self.root==1:
                datas = {  "type":"exejs","data":"set_Meitu_bg_play({0})".format( {"data":self.fill,"time":self.TIME,"len":len(self.fill)})   }
                self.send( MsgType=MsgType.Text, Receiver="Screen", Data= datas )
            if self.root==2:
                datas = {  "type":"exejs","data":"set_Meitu_Fullscreen_play({0})".format( {"data":self.fill,"time":self.TIME,"len":len(self.fill)})   }
                self.send( MsgType=MsgType.Text, Receiver="Screen", Data= datas )
            self.Refresh(self.root,self.start_root)

        if isinstance(data, dict):
            if data['initstate'] == 'onLoad':
                self.Refresh(self.root,self.start_root)

        if "美图开机" in data:
            self.setfill({"root":self.root,"time":self.TIME},"w")
            self.Refresh(self.root,1)
            self.start_root=1

        if "美图关机" in data:
            self.setfill({"root":0,"time":2},"w")
            self.Refresh(self.root,0)
            self.start_root=0
